import { getUserCarList, addOrUpdateAmount, deleteUserCar } from "@/service/api/member.js"
import store from "@/store/index.js"
let getCartTimer = null
const cartUtil = {
    //获取总数
    getCartTotal(data, callBack) {
        let total = {
            totalNum: 0,
            totalPrice: 0,
            totalSpace: 0,
        }
        let space = []
        data.forEach((e) => {
            e.SubItems.forEach((item) => {
                total.totalNum += item.Amount
                total.totalPrice += item.V_FactPrice * item.Amount
                //统计种类
                let findSpace = space.find((productId) => {
                    return productId == item.ProductId
                })
                if (!findSpace) {
                    space.push(item.ProductId)
                }
            })
        })
        total.totalPrice = total.totalPrice.toFixed(2)
        total.totalSpace = space.length
        callBack(total)
    },
    //获取选择总数
    getCartTotalForSelect(data, ids, callBack) {
        let total = {
            totalNum: 0,
            totalPrice: 0,
            totalSpace: 0,
        }
        let space = []
        //数据处理
        data.forEach((e) => {
            e.SubItems.forEach((item) => {
                let findResult = ids.find((id) => {
                    return id == String(item.ID)
                })
                if (findResult) {
                    //统计数量
                    total.totalNum += item.Amount
                    //统计金额
                    total.totalPrice += item.V_FactPrice * item.Amount
                    //统计种类
                    let findSpace = space.find((productId) => {
                        return productId == item.ProductId
                    })
                    if (!findSpace) {
                        space.push(item.ProductId)
                    }
                }
            })
        })
        total.totalPrice = total.totalPrice.toFixed(2)
        total.totalSpace = space.length
        callBack(total)
    },
    //获取购物车
    getUserCarList: (isSet = true) => {
        return new Promise((resolve, reject) => {
            let param = {
                priceType: 1,
            }
            getUserCarList(param).then((res) => {
                let items = []
                if (res && res.Data && res.Data.length > 0) {
                    res.Data.forEach((e) => {
                        let filterItem = items.filter((item) => {
                            return item.ProductId == e.ProductId
                        })
                        if (filterItem.length < 1) {
                            let SubItems = []
                            SubItems.push(e)
                            let obj = {
                                ProductId: e.ProductId,
                                V_Title: e.V_Title,
                                V_ThumbnailUrl160: e.V_ThumbnailUrl160,
                                SubItems: SubItems,
                            }
                            items.push(obj)
                        } else {
                            filterItem[0].SubItems.push(e)
                        }
                    })
                }
                resolve(items)
                if (isSet) {
                    store.commit("SET_CART", items)
                }
            })
        })
    },
    //增量更新需要先去找购物车，匹配后增加份数
    getNewParam(param) {
        if (store.state.cartData && store.state.cartData.length > 0) {
            param.forEach((pa) => {
                store.state.cartData.forEach((cart) => {
                    if (cart.ProductId == pa.ProductId) {
                        cart.SubItems.forEach((sub) => {
                            if (sub.UnitId == pa.UnitId && sub.SkuId == pa.SkuId) {
                                pa.Amount += sub.Amount
                            }
                        })
                    }
                })
            })
        }
        return param
    },
    /**
     * 更新购物车
     * param 参数
     * isAdd 是否是增量更新
     */
    updateCart: (param, isAdd = false, isTime = true) => {
        return new Promise((resolve, reject) => {
            if (isAdd) {
                param = cartUtil.getNewParam(param)
            }
            if (isTime) {
                clearTimeout(getCartTimer)
                getCartTimer = setTimeout((e) => {
                    addOrUpdateAmount(param).then((res) => {
                        cartUtil.getUserCarList().then((res) => {
                            // uni.hideLoading()
                            resolve(res)
                        })
                    })
                }, 300)
            } else {
                addOrUpdateAmount(param).then((res) => {
                    cartUtil.getUserCarList(isTime).then((res) => {
                        // uni.hideLoading()
                        resolve(res)
                    })
                })
            }
        })
    },
    //删除、清空购物车
    deleteUserCar: (param) => {
        return new Promise((resolve, reject) => {
            deleteUserCar(param).then((res) => {
                cartUtil.getUserCarList().then((res) => {
                    resolve(true)
                })
            })
        })
    },
}
module.exports = {
    getCartTotal: cartUtil.getCartTotal,
    getCartTotalForSelect: cartUtil.getCartTotalForSelect,
    updateCart: cartUtil.updateCart,
    deleteUserCar: cartUtil.deleteUserCar,
    getUserCarList: cartUtil.getUserCarList,
}
